package com.zgs.mall.coupon.service.impl;

import com.zgs.mall.coupon.entity.SeckillSkuRelationEntity;
import com.zgs.mall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zgs.common.utils.PageUtils;
import com.zgs.common.utils.Query;

import com.zgs.mall.coupon.dao.SeckillSessionDao;
import com.zgs.mall.coupon.entity.SeckillSessionEntity;
import com.zgs.mall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {
    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLates3DaySession() {
      //开始
        List<SeckillSessionEntity> sessions = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", getStartTime(), getEndTime()));
        List<SeckillSessionEntity> collect = sessions.stream().map(sesion -> {
            Long id = sesion.getId();
            List<SeckillSkuRelationEntity> skuRelationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
            sesion.setSeckillSkuRelations(skuRelationEntities);
            return sesion;
        }).collect(Collectors.toList());
        return collect;
    }
    private String getStartTime(){
        LocalDate now = LocalDate.now();
        LocalTime min =   LocalTime.MIN;
        LocalDateTime of = LocalDateTime.of(now, min);
        return  of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    }
    private String getEndTime(){
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(3);
        LocalTime max =   LocalTime.MAX;
        return LocalDateTime.of(localDate,max).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(3);
        LocalTime min =   LocalTime.MIN;
        LocalTime max =   LocalTime.MAX;

        System.out.println(localDate);

    }
}